$(function() {

    var arrayStr = JSON.parse(localStorage.getItem('mark'))
    var code = localStorage.getItem('city')
    if(code!=null && arrayStr!=null ){
    	console.log('INIT')
    		init();
    }

    function init(){
	    var ArrayPos = new Array();
	    var ArrayTime = new Array();
	    var NewArray = new Array();
	    var nameHtml = '';
	    var Timer = 0;
	    var distance = 0;
	    var firstSub=0 //第一个下标

	    //获取数据 初始化 二维数组 生成景点到即景点的数据
	    for (var i = 0; i < arrayStr.length; i++) {
	        for (var j = i + 1; j < arrayStr.length; j++) {
	            var data = {
	                start: arrayStr[i].position,
	                startid: arrayStr[i].id,
	                end: arrayStr[j].position,
	                endid: arrayStr[j].id,
	            }
	            ArrayPos.push(data)
	        }
	    }

	    // 遍历每个每个景点到景点，获取 路线 时间
	    for (var i = 0; i < ArrayPos.length; i++) {
	        var j = 0;
	        (function(i) {
	            //获取时间，路径
	            var url = "http://restapi.amap.com/v3/direction/transit/integrated?key=b924bdcb52ba818af5dc34c21ba657fa&s=rsv3"
	            app.ajax(url + '&city=' + code + '&origin=' + (ArrayPos[i].start).toString() + '&destination=' + (ArrayPos[i].end).toString(), 'GET', {}, function(res) {
	                if (res.infocode == "10000") {
	                    var data = res.route.transits;
	                    // var dataold
	                    if (data.length <= 0) {
	                        dataold = {
	                            startid: ArrayPos[i].startid,
	                            endid: ArrayPos[i].endid,
	                            distance: 0,
	                            duration: 0
	                        }
	                    } else {
	                        // console.log(data[0])
	                        dataold = {
	                            startid: ArrayPos[i].startid,
	                            endid: ArrayPos[i].endid,
	                            distance: data[0].distance,
	                            duration: data[0].duration
	                        }
	                    }
	                    ArrayTime.push(dataold);
	                    j++
	                    if (j == ArrayPos.length) {
	                        console.log(ArrayTime)
	                        generate();
	                    }

	                }
	            });
	        })(i)
	    }

	    //生成最佳路线 1下标唯一
	    function generate() {
	    	console.log('ok')
	        var sub, subMax = 100000;
	        for (var y = 0; y < ArrayTime.length; y++) {
	            if (ArrayTime[y].startid == firstSub) {
	                if (ArrayTime[y].duration < subMax) {
	                    // 路程
	                    // subMax=ArrayTime[y].distance;
	                    // 时间
	                    subMax = ArrayTime[y].duration;
	                    sub = y;
	                }
	            }
	        }
	        // console.log(ArrayTime)
	        NewArray.push(ArrayTime[sub])
	        for (var j=0; j < ArrayTime.length; j++) {
	            // console.log(ArrayTime[j])
	            if (ArrayTime[j].startid == firstSub) {
	                ArrayTime.del(j);
	                j = -1;
	            }
	        }
	         console.log(ArrayTime)
	        if (ArrayTime.length <= 1) {
	            NewArray.push(ArrayTime[0])
	            // console.log(NewArray)
	            for (var x = 0; x < NewArray.length; x++) {
	                if (x < NewArray.length - 1) {
	                    NewArray[x].startid
	                    Timer += parseInt(NewArray[x].duration);
	                    distance += parseInt(NewArray[x].distance);
	                    nameHtml += arrayStr[NewArray[x].startid ].name + '——>'
	                } else {
	                    console.log(NewArray[x].startid)
	                    Timer += parseInt(NewArray[x].duration);
	                    distance += parseInt(NewArray[x].distance);
	                    nameHtml += arrayStr[NewArray[x].startid].name + '——>'
	                    nameHtml += arrayStr[NewArray[x].endid].name;
	                }
	            }
	            $('.lucheng').html('全长约：' + (distance / 1000).toFixed(2)+ "km")
	            $('.shijian').html("大约需要：" + ((Timer) / 60).toFixed(2)+ '分钟')
	            $('.paixu').html(nameHtml)

	        }
	    }

    }

})

//通过下标删除数组相应的字段
Array.prototype.del = function(dx) {
    if (isNaN(dx) || dx > this.length) { return false; }
    this.splice(dx, 1);
}